#ifndef QWALKMANOBJECT_H
#define QWALKMANOBJECT_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMediaPlayer>
#include <QFile>

class QWalkmanObject : public QObject
{
    Q_OBJECT
public:
    explicit QWalkmanObject(QObject *parent = nullptr, bool isAutoPlay = true);
    ~QWalkmanObject();

signals:
    void songChanged(const QString &_songName);
    void downloadFinished(const QString &fileName);
    void playSong(const QUrl &url);
public:
    /**
     * @brief startMusic
     * @param _musicType 热歌榜/新歌榜/飙升榜/抖音榜/电音榜
     */
    void startMusic(const QString &_musicType = "抖音榜");
    void playAndPause();
    void dowloadCurrent();
private:
    void checkDir(const QString &_path);
    void parseJsonData(const QByteArray &_byData);
    void downloadFile(const QUrl &url);

private slots:
    void sltHttpRead();
    void sltHttpFinished();

    void durationChanged(qint64 duration);
    void positionChanged(qint64 position);
    void error(QMediaPlayer::Error error);

private:
    QNetworkAccessManager   *m_networkManager;
    QNetworkReply           *m_networkDownlad;
    QMediaPlayer            *m_medialplayer;
    qint64                   m_duration;

    bool                     m_isAutoDownload;
    QFile                   *m_localFile;
    QString                  m_strFilePath;
    QString                  m_strFileName;
    QUrl                     m_url;
};

#endif // QWALKMANOBJECT_H
